package com.et114.components.xfire;


	import java.net.MalformedURLException;

	import org.apache.commons.logging.Log;
	import org.apache.commons.logging.LogFactory;
	import org.codehaus.xfire.annotations.AnnotationServiceFactory;
	import org.codehaus.xfire.client.XFireProxyFactory;
	import org.codehaus.xfire.service.Service;
	import org.codehaus.xfire.service.binding.ObjectServiceFactory;
	import org.springframework.util.Assert;

	/**
	 * XFire Client 工厂类.
	 * @author guanhw
	 */
	public class XFireClientFactory {
		private static XFireProxyFactory serviceFactory = new XFireProxyFactory();
		private static final Log log = LogFactory.getLog( XFireClientFactory.class );

		private XFireClientFactory() {
		}

		/**
		 * 获得POJO形式的Web服务的客户端Proxy类.
		 *
		 * @param serviceURL   Web ServiceURL
		 * @param serviceClass Web Service接口类
		 * @return 类型为Web Service接口的客户端Proxy类
		 */
		public static Object getClient( String serviceURL, Class serviceClass ) {
			Assert.notNull( serviceURL );
			Assert.notNull( serviceClass );
			Service serviceModel = new ObjectServiceFactory().create( serviceClass );
			try {
				return  ( Object  )serviceFactory.create( serviceModel, serviceURL );
			} catch ( MalformedURLException e ) {
				log.error( e.getMessage(), e );
				return null;
			}
		}

		/**
		 * 获得JSR181形式定义的Web服务的客户端Proxy类. 注意和普通的生成客户端方法不同,需要多一个implClass参数.
		 *
		 * @param serviceURL   Web ServiceURL.
		 * @param serviceClass Web Service接口类.
		 * @param implClass	Web Service街口的服务端实现类.
		 * @return 类型为Web Service接口的客户端Proxy类.
		 */
		public static Object getJSR181Client( String serviceURL , Class serviceClass , Class implClass ) {
			Assert.notNull( serviceURL );
			Assert.notNull( serviceClass );
			Service serviceModel = new AnnotationServiceFactory().create( implClass );
			try {
				return ( Object ) serviceFactory.create( serviceModel , serviceURL );
			} catch ( MalformedURLException e ) {
				log.error( e.getMessage() , e );
				return null;
			}
		}
	
}
